import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:flutter_base_app/config/i18n/i18n.dart';

import '../../../config/app_colors.dart';
import '../../../config/i18n/i18n_key.dart';
import '../../../dialog/common/tips_dialog.dart';

extension BDStringExtend on String {
  void showTipsDialog({
    String? title,
    bool isShowCancel = false,
    bool clickMaskDismiss = true,
    bool? backDismiss,
    Function(bool)? onResult,
    Function()? onDismissCall,
  }) =>
      showAlertDialog(
        this,
        isShowCancel: isShowCancel,
        backDismiss: backDismiss,
        onResult: onResult,
        onDismissCall: onDismissCall,
        clickMaskDismiss: clickMaskDismiss,
      );
// TipsDialog(
//   text: this,
//   title: title,
//   width: width,
//   height: height,
//   iconWidget: iconWidget,
//   backDismiss: backDismiss,
//   onResult: onResult,
//   isShowCancel: isShowCancel,
//   onDismissCall: onDismissCall,
// )..show();
}

class BaseDialogSys extends Dialog {
  //点击背景是否能够退出
  final bool barrierDismissible;

  const BaseDialogSys({super.key, this.barrierDismissible = true});

  @mustCallSuper
  @override
  Widget build(BuildContext context) {
    return Container();
  }

  T show<T extends BaseDialogSys>(BuildContext context) {
    showDialog(
        context: context,
        // 点击遮罩不关闭对话框
        barrierDismissible: false,
        builder: (context) {
          return this;
        });
    return this as T;
  }
}

//关闭全部的对话框
void dismissAllDialog() {
  //关闭全部SmartDialog对话框
  while (SmartDialog.checkExist()) {
    SmartDialog.dismiss(force: true);
  }
  // 关闭原生对话框
  Navigator.popUntil(Get.context!, (route) => route is! DialogRoute);
}

//对话框底部两个按钮
class DialogCommonAction extends StatelessWidget {
  final ValueChanged<bool> onResult;
  final bool showCancel;
  final String? leftText;
  final String? rightText;

  const DialogCommonAction({
    super.key,
    required this.onResult,
    this.showCancel = true,
    this.leftText,
    this.rightText,
  });

  @override
  Widget build(BuildContext context) {
    //两个按钮
    return SizedBox(
        width: double.infinity,
        height: 50,
        child: Row(
          children: [
            if (showCancel)
              Expanded(
                child: FilledButton(
                  onPressed: () {
                    onResult(false);
                  },
                  style: FilledButton.styleFrom(backgroundColor: ColorsApp.gray_e5),
                  child: Text(leftText ?? I18nKey.cancel.trx, style: TextStyle(color: ColorsApp.black, fontSize: 16)),
                ),
              ),
            if (showCancel) SizedBox(width: 10),
            Expanded(
              child: FilledButton(
                onPressed: () async {
                  onResult(true);
                },
                style: FilledButton.styleFrom(backgroundColor: ColorsApp.black_33),
                child: Text(rightText ?? I18nKey.ok.trx, style: TextStyle(color: ColorsApp.white, fontSize: 16)),
              ),
            )
          ],
        ));
  }
}
